package cn.zhaoyuening.fanblog.admin.web;

import java.util.List;

import cn.zhaoyuening.fanblog.admin.utils.ResultObj;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import cn.zhaoyuening.fanblog.admin.service.ArticleTypeService;
import cn.zhaoyuening.fanblog.common.domain.ArticleType;

@RestController
@RequestMapping(path = "admin")
public class AdminArticleTypeController {
	@Autowired
	private ArticleTypeService articleTypeService;
	
	@RequestMapping(path = "articleTypes",method = RequestMethod.GET)
	public List<ArticleType> findAll(){
		return articleTypeService.findAll();
	}


	@RequestMapping(path = "articleType/{id}", method = RequestMethod.PUT)
	public ResultObj updateType(@RequestBody ArticleType type, @PathVariable Long id) {
		type.setId(id);
		boolean result = articleTypeService.update(type);
		return ResultObj.getByResult(result);
	}

	@RequestMapping(path = "articleType", method = RequestMethod.DELETE)
	public ResultObj deleteTypeByList(@RequestBody List<ArticleType> typeList) {
		articleTypeService.deleteByList(typeList);
		return ResultObj.ok();
	}

	@RequestMapping(path = "articleType", method = RequestMethod.POST)
	public ResultObj addArticleType(String name) {
		ArticleType articleType = new ArticleType().setName(name).setCount(0l);
		boolean result = articleTypeService.add(articleType);
		return ResultObj.getByResult(result);
	}

}
